package com.jx.lcs.datareport.web;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jx.lcs.datareport.service.UserDeptReportService;
import com.tianyu.jty.common.utils.DateUtils;
import com.tianyu.jty.common.web.BaseController;

@Controller
@RequestMapping("userDeptReport")
public class UserDeptReportController extends BaseController {

  private static final Logger logger = LoggerFactory.getLogger(UserDeptReportController.class);

  @Autowired
  private UserDeptReportService userDeptReportService;

  @RequestMapping(method = RequestMethod.GET)
  public String list() {
    return "report/userDeptReport";
  }

  /**
   * 根据部门统计B端用户--统计表数据
   */
  @RequestMapping(value = "getTableData", method = RequestMethod.GET)
  @ResponseBody
  public Map<String, Object> getTableData() {
    Map<String, Object> map = new HashMap<String, Object>();

    try {
      List<Map<String, Object>> userData = userDeptReportService.countUserByDept();
      map.put("total", 0);
      map.put("rows", userData);
    } catch (Exception e) {
      logger.error("--获取用户相关数据报表出现异常！", e);
    }

    return map;
  }

  /**
   * 根据部门统计B端用户--统计图数据
   */
  @RequestMapping(value = "getChartData", method = RequestMethod.GET)
  @ResponseBody
  public Map<String, List<?>> getChartData() {
    Map<String, List<?>> result = new HashMap<>();

    try {
      List<Map<String, Object>> userData = userDeptReportService.countUserByDept();

      List<Object> category = new ArrayList<>();
      List<Object> value = new ArrayList<>();

      Pattern pattern = Pattern.compile("(?<=.)(?=(?:......)+\\b)");
      Matcher matcher = pattern.matcher("");
      for (Map<String, Object> loopVariable : userData) {
        matcher.reset(StringUtils.reverse(String.valueOf(loopVariable.get("userDept"))));
        category.add(StringUtils.reverse(matcher.replaceAll("\n")));
        value.add(loopVariable.get("totall"));
      }

      Collections.reverse(category);
      Collections.reverse(value);
      result.put("category", category);
      result.put("value", value);
    } catch (Exception e) {
      logger.error("--获取用户相关数据报表出现异常！", e);
    }

    return result;
  }

  /**
   * 导出统计数据
   */
  @RequestMapping(value = "export", method = RequestMethod.GET,
      produces = "text/plain;charset=utf-8")
  @ResponseBody
  public void export(HttpServletResponse response) throws IOException {

	String fileName = "各营业部理财师用户量数据-" + DateUtils.formatDate(DateUtils.getSysDate(), "yyyyMMdd");
	
    response.setContentType("text/html;charset=utf-8");
    response.setContentType("application/x-msdownload;");
    response.setHeader("Content-disposition",
        "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")
            + ".xls");

    userDeptReportService.exportDataReport(response.getOutputStream());

  }
}
